package com.rdc.example.service.impl;

import com.rdc.example.bean.Result;
import com.rdc.example.dao.ArticleDao;
import com.rdc.example.dao.CommentDao;
import com.rdc.example.dao.impl.CommentDaoImpl;
import com.rdc.example.dao.impl.CommentLikeDaoImpl;

/**
 * @author duck
 */
public class CommentLikeServiceImpl {

    CommentLikeDaoImpl commentLikeDao = new CommentLikeDaoImpl();

    public static void main(String[] args) throws Exception {
        CommentLikeServiceImpl commentLikeService = new CommentLikeServiceImpl();
        System.out.println(commentLikeService.turnCommentLike(1,1));
    }

    public Result turnCommentLike(Integer userId, Integer commentId) throws Exception {
        try {
            if (userId == null || commentId == null) {
                return null;
            }
            CommentDao commentDao = new CommentDaoImpl();
            if (commentLikeDao.likeCheck(userId, commentId)) {
                //  点赞了，取消，comment.Like--
                boolean registerLike = commentLikeDao.deleteLike(userId, commentId);
                boolean registerComment = commentDao.reduceLike(commentId);
                return registerLike && registerComment ? Result.success("取消成功", null)
                        : Result.fail("取消失败", null);
            } else {
                // 没点赞，要点，comment.Like++
                boolean registerLike = commentLikeDao.insertLike(userId, commentId);
                boolean registerComment = commentDao.addLike(commentId);
                return registerLike && registerComment ? Result.success("点赞成功", null)
                        : Result.fail("点赞失败", null);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
